package edu.whut.mall.admin.controller;

import edu.whut.mall.admin.service.ISysRoleResourceService;
import edu.whut.mall.common.api.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;


/**
 * @program: d9lab-mall
 * @description: 角色和资源的对应控制器
 * @author: Wayne
 * @create: 2020-04-17 14:03
 **/
@Slf4j
@Api(value="角色资源管理器",tags = {"角色资源管理器"})
@RestController
@RequiredArgsConstructor
@RequestMapping("/roleResource")
public class RestRoleResourceController {

    private final ISysRoleResourceService roleResourceService;

    @ApiOperation("指定角色资源")
    @PostMapping("/assignResourceToRole")
    public CommonResult assignResourceToRole(@NonNull @RequestParam Long roleId,
                                             @NotBlank @RequestParam String resourceIdString){
        int result = roleResourceService.assignResource2Role(roleId, resourceIdString);
        if (result == 0) {
            return CommonResult.failed("批量插入失败，数据库插入有问题");
        }
            return CommonResult.success("为角色指定资源成功");

    }

}
